﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using DevExpress.XtraEditors.Controls;
using ECRMS.WinForm.WinService;
using ECRMS.WinForm.BaseClass.Common;
using ECRMS.WinForm.Properties;
using System.Linq;
using ECRMS.WinForm.BaseClass.Other;

namespace ECRMS.WinForm.ModuleEdits
{
    public partial class EmployeeEdit : BaseEdit
    {
        public EmployeeEdit(Rectangle r, OperationEnum op)
        {
            InitializeComponent();
            InitView(r, op);
            InitData();
        }

        private void InitView(Rectangle r, OperationEnum op)
        {
            base.NewBarButtonVisiable = false;
            base.SaveBarButtonVisiable = true;
            base.EditBarButtonVisiable = false;
            base.DelBarButtonVisiable = false;
            base.RefreshBarButtonVisiable = true;
            base.ResetBarButtonVisiable = true;
            base.OtherBarButtonVisiable = false;
            base.Other1ButtonVisiable = false;
            base.Op = op;

            this.Text = "组织架构编辑";
            this.Left = r.Left + (r.Width - this.Width) / 2;
            this.Top = r.Top + (r.Height - this.Height) / 2;
        }

        private void InitData()
        {
          
            switch (base.Op)
            {
                case OperationEnum.New:
                    break;
                case OperationEnum.Modify:
                    txt_name.Text = core.CoreEmployee.OperatorRow.UserInfo.Name;
                    rgp_gender.EditValue = core.CoreEmployee.OperatorRow.Gender == "F" ? false : true;
                    txt_username.Text = core.CoreEmployee.OperatorRow.UserInfo.Username;
                    txt_password.Text = core.CoreEmployee.OperatorRow.UserInfo.Password;
                    bte_structure.Text = core.CoreEmployee.OperatorRow.UserInfo.StructureName;
                    txt_email.Text = core.CoreEmployee.OperatorRow.Email;

                    ccb_basic.SetEditValue(GetAuthModuleString(11,19));
                    ccb_index.SetEditValue(GetAuthModuleString(21, 29));
                    ccb_report.SetEditValue(GetAuthModuleString(31, 39));
                    ccb_new.SetEditValue(GetAuthModuleString(41, 49));
                    ccb_yuji.SetEditValue(GetAuthModuleString(51, 59));
                   
                    break;
                case OperationEnum.Null:
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private string GetAuthModuleString(int start, int end)
        {
            string result = "";
            List<int> list = core.CoreEmployee.OperatorRowCtFunctions.Where(p => p >= start && p <= end).ToList();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == 0)
                {
                    result += list[i].ToString();
                }
                else
                    result = result + "," + list[i].ToString();
            }
            return result;
        }

        public override void saveBarButtonClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.saveBarButtonClick(sender, e);
            core.CoreEmployee.OperatorRow.Email = txt_email.Text.Trim();
            core.CoreEmployee.OperatorRow.Gender = (bool)rgp_gender.EditValue == true ? "M" : "F";
            core.CoreEmployee.OperatorRow.UserInfo.CorporationRefId = core.CoreUser.UserInfo.CorporationRefId;
            core.CoreEmployee.OperatorRow.UserInfo.Name = txt_name.Text.Trim();
            core.CoreEmployee.OperatorRow.UserInfo.Password = txt_password.Text.Trim();
            core.CoreEmployee.OperatorRow.UserInfo.Username = txt_username.Text.Trim();

            //权限设置
            core.CoreEmployee.OperatorRowCtFunctions.Clear();
            core.CoreEmployee.OperatorRowCtFunctions = GetAuthModelus();
            core.CoreEmployee.OperatorRow.Functions = core.CoreEmployee.OperatorRowCtFunctions;
            //前面判断完成 开始访问服务
            core.CreateWaitDialog("数据提交中...", "保存数据", 150, 50, false, this, this);
            FuncResult result = new FuncResult();
            switch (base.Op)
            {
                case OperationEnum.New:
                    result = core.CoreEmployee.InsertEmployee();

                    break;
                case OperationEnum.Modify:
                    result = core.CoreEmployee.UpdateEmployee();

                    break;
                default:
                    break;
            }
            if (result.Status == 0)
            {
                XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                core.CloseWaitDialog(this, true);
                return;
            }
            core.CloseWaitDialog(this, true);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private List<int> GetAuthModelus()
        {
            List<int> result = new List<int>();
            if (ccb_basic.EditValue != "")
                foreach (string id in ccb_basic.EditValue.ToString().Split(','))
                {
                    result.Add(Convert.ToInt32(id));
                }
            if (ccb_index.EditValue != "")
                foreach (string id in ccb_index.EditValue.ToString().Split(','))
                {
                    result.Add(Convert.ToInt32(id));
                }
            if (ccb_report.EditValue != "")
                foreach (string id in ccb_report.EditValue.ToString().Split(','))
                {
                    result.Add(Convert.ToInt32(id));
                }
            if (ccb_new.EditValue != "")
                foreach (string id in ccb_new.EditValue.ToString().Split(','))
                {
                    result.Add(Convert.ToInt32(id));
                }
            if (ccb_yuji.EditValue != "")
                foreach (string id in ccb_yuji.EditValue.ToString().Split(','))
                {
                    result.Add(Convert.ToInt32(id));
                }
            return result;
        }

        private void bte_position_Click(object sender, EventArgs e)
        {
            StructureList strList = new StructureList(new Rectangle(this.Left, this.Top, this.Width, this.Height));
            DialogResult result = strList.ShowDialog();

            if (result == DialogResult.OK)
            {
                bte_structure.Text = core.CoreEmployee.OperatorRow.UserInfo.StructureName;
            }
            strList.Dispose();
        }

    }
}